package gamagora.game.states
{
	import gamagora.game.utils.OSFPSTimer;
	import gamagora.game.world.OSWorld;
	import gamagora.game.world.maps.OSMap;
	
	import org.flixel.FlxG;
	import org.flixel.FlxSprite;
	import org.flixel.FlxState;
	import org.flixel.FlxText;

	public class OSPlayState extends FlxState
	{	
		private static const DEBUG:Boolean = false;
		private static var debugTexts:Vector.<FlxText> = new Vector.<FlxText>();
		
		private var _fpsTimer:OSFPSTimer;
		private var _fps:FlxText;
		private var _blackFade:FlxSprite;
		private var _completeFade:Boolean;
		private var _world:OSWorld;
	
		public override function create():void
		{
			//FPS
			_fpsTimer = new OSFPSTimer();
			_fps = addDebugText();
			
			//World
			_world = new OSWorld();
			add(_world);
			
			if(DEBUG)
			{
				for(var i:uint;i<debugTexts.length;i++)
				{
					add(debugTexts[i]);
				}
				
				FlxG.mouse.show();
			}
			
			//Black fade
			_blackFade = new FlxSprite();
			_blackFade.makeGraphic(FlxG.width, FlxG.height * 2, 0xff000000);
			add(_blackFade);
		}
		
		public override function update():void
		{
			if(!_completeFade)
			{
				_blackFade.alpha -= FlxG.elapsed;
				if(_blackFade.alpha <= 0)
				{
					remove(_blackFade, true);
					_completeFade = true;
				}
			}
			
			if(FlxG.keys.ESCAPE)
			{
				FlxG.switchState(new OSMenuState);
			}
			
			super.update();
			
			//Infos
			if(DEBUG)
			{
				_fpsTimer.update();
				_fps.text = _fpsTimer.fps.toFixed(1) + " fps";
			}
		}
		
		public override function destroy():void
		{
			super.destroy();
			debugTexts.splice(0, debugTexts.length);
		}
		
		//Debugs
		public static function addDebugText():FlxText
		{
			var text:FlxText = new FlxText(5, 5 + (debugTexts.length * 15), 400).setFormat(null, 10, 0xffffff, "left", 0x233e58);
			text.scrollFactor.x = text.scrollFactor.y = 0;
			debugTexts.push(text);
			return text;
		}
	}
}
